package com.shawn.mobliesafe;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.shawn.service.AddressService;
import com.shawn.service.BlackNumService;
import com.shawn.service.WatchDogService;
import com.shawn.ui.SettingClickView;
import com.shawn.ui.SettingView;
import com.shawn.utils.AdressUtils;

public class SettingActivity extends AppCompatActivity {

    private SettingView sv_setting_update;
    private SettingView sv_setting_address;
    private SettingView sv_setting_blacknum;
    private SettingView sv_setting_lock;
    private SettingClickView scv_setting_changedbg;
    private SettingClickView scv_setting_location;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        sv_setting_update = (SettingView)findViewById(R.id.sv_setting_update);
        sv_setting_address = (SettingView)findViewById(R.id.sv_setting_address);
        sv_setting_blacknum = (SettingView)findViewById(R.id.sv_setting_blacknum);
        scv_setting_changedbg = (SettingClickView)findViewById(R.id.scv_setting_changedbg);
        scv_setting_location = (SettingClickView)findViewById(R.id.scv_setting_location);
        sv_setting_lock = (SettingView)findViewById(R.id.sv_setting_lock);
        //name : 保存信息的文件的名称
        //mode : 权限
        sp = getSharedPreferences("config",MODE_PRIVATE);

        update();
        changedbg();
        location();

    }

    /**
     * 设置归属地提示框风格
     */
    private void changedbg() {
        final String[] items={"半透明","活力橙","卫士蓝","金属灰","苹果绿"};
        //设置标题和描述信息
        scv_setting_changedbg.setTitle("归属地提示框风格");
        scv_setting_changedbg.setDes(items[sp.getInt("which", 0)]);//根据保存的选中的选项的索引值设置自定义组合控件描述信息回显操作
        scv_setting_changedbg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(SettingActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("归属地提示框风格")
                        .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putInt("which", which);
                                editor.commit();
                                scv_setting_changedbg.setDes(items[which]);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });

    }

    private void location() {
        scv_setting_location.setTitle("归属地提示框位置");
        scv_setting_location.setDes("设置归属地提示框的显示位置");
        scv_setting_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this,DragViewActivity.class);
                startActivity(intent);
            }
        });
    }

    private void update() {
        if(sp.getBoolean("update",true)) {
            sv_setting_update.setChecked(true);
        }else {
            sv_setting_update.setChecked(false);
        }

        //设置自定义组合控件的点击事件
        //问题1:点击checkbox发现描述信息没有改变,原因:因为checkbox天生是有点击事件和获取焦点事件,当点击checkbox,这个checkbox就会执行他的点击事件
        //而不会执行条目的点击事件
        //问题2:没有保存用户点击操作
        sv_setting_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sp.edit();
                if (sv_setting_update.isChecked()) {
                    sv_setting_update.setChecked(false);
                    editor.putBoolean("update", false);
                } else {

                    sv_setting_update.setChecked(true);
                    //保存状态
                    editor.putBoolean("update", true);
                }
                editor.commit();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        address();
        blackNum();
        lock();
    }

    /**
     * 软件锁
     */
    private void lock() {
        final boolean isStart = AdressUtils.isRunningService("com.shawn.service.WatchDogService",this);
        if(isStart) {
            sv_setting_lock.setChecked(true);
        }else {
            sv_setting_lock.setChecked(false);
        }
        sv_setting_lock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, WatchDogService.class);
                if(sv_setting_lock.isChecked()) {
                    stopService(intent);
                    sv_setting_lock.setChecked(false);
                }else {
                    startService(intent);
                    sv_setting_lock.setChecked(true);
                }
            }
        });
    }

    /**
     * 黑名单拦截
     */
    private void blackNum() {
        if(AdressUtils.isRunningService("com.shawn.service.BlackNumService",this)) {
            sv_setting_blacknum.setChecked(true);
        }else {
            sv_setting_blacknum.setChecked(false);
        }

        sv_setting_blacknum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, BlackNumService.class);
                if (sv_setting_blacknum.isChecked()) {
                    stopService(intent);
                    sv_setting_blacknum.setChecked(false);
                }else {
                    startService(intent);
                    sv_setting_blacknum.setChecked(true);
                }
            }
        });
    }

    /**
     *  显示号码归属地
     */
    private void address() {
        if(AdressUtils.isRunningService("com.shawn.service.AddressService",this)) {
            sv_setting_address.setChecked(true);
        }else {
            sv_setting_address.setChecked(false);
        }

        sv_setting_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SettingActivity.this, AddressService.class);
                if (sv_setting_address.isChecked()) {
                    stopService(intent);
                    sv_setting_address.setChecked(false);
                }else {
                    startService(intent);
                    sv_setting_address.setChecked(true);
                }
            }
        });
    }
}
